package com.shengde.jinwawaforleader.model.myJob.classList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.base.CommonBaseAdapter;
import com.shengde.jinwawaforleader.base.ViewHolder;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.model.myJob.classStatus.AssignmentsAndCourseDetailsActivity;
import com.shengde.jinwawaforleader.model.myJob.classStatus.ClassWorkLookPeopleNumActivity;
import com.shengde.jinwawaforleader.model.myJob.classStatus.LookPeopleNumActivity;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.BasexUtilsProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProClassWorkInfo;
import com.shengde.jinwawaforleader.utils.ActivityHelper;
import com.shengde.jinwawaforleader.utils.DateUtil;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * Created by Chason on 2016/12/23.
 */

public class ClassWorkActivity extends BaseActivity{

    private TextView tv_back,tv_title;
    private ListView lv;
    private PullToRefreshListView ptr_listview;
    private CommonBaseAdapter<ProClassWorkInfo.DynamicEntity> adapter;
    private List<ProClassWorkInfo.DynamicEntity> dynamicEntityList;
    private String teacherid;
    private boolean isFirstIn = true;
    private int tag = 0;
    private String time;

    @Override
    public void onRoot(Bundle savedInstanceState) {
        setContentView(R.layout.activity_classwork);
    }

    @Override
    public void initView() {
        tv_back = (TextView)findViewById(R.id.tv_back);
        tv_title = (TextView)findViewById(R.id.tv_title);
        ptr_listview = (PullToRefreshListView)findViewById(R.id.ptr_listview);
        lv = ptr_listview.getRefreshableView();
    }

    @Override
    public void initData() {
        teacherid = getIntent().getStringExtra("teacherid");
        tv_title.setText("班级工作");
        time = System.currentTimeMillis()+"";
        time = time.substring(0,10);
        getSectionDynamicRequest();
    }

    @Override
    public void initListener() {
        tv_back.setOnClickListener(this);

        //刷新监听
        ptr_listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                tag = 2;
                time = System.currentTimeMillis()+"";
                time = time.substring(0,10);
                getSectionDynamicRequest();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                tag = 1;
                time = dynamicEntityList.get(dynamicEntityList.size()-1).getLasttime()+"";
                time = time.substring(0,10);
                getSectionDynamicRequest();
            }
        });
    }

    @Override
    public void onWidgetClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
        }
    }

    public void getSectionDynamicRequest(){
        String url = "";
        if (User.isTest) {
            url = BaseProtocol.HOST_BASE_TEST + "/Dynamic/sectionDynamic";
        } else {
            url = BaseProtocol.HOST_BASE + "/Dynamic/sectionDynamic";
        }
        if(isFirstIn) {
            BasexUtilsProtocol.dialog = ActivityHelper.createLoadingDialog(this);
            BasexUtilsProtocol.dialog.show();
        }
        RequestParams params = BaseProtocol.getRequestParams(url);
        params.addParameter("userid", teacherid);
        params.addParameter("uninx", time);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                ProClassWorkInfo info = new Gson().fromJson(result, ProClassWorkInfo.class);
                if(info!=null&&info.getCode()==0) {
                    if(tag == 1) {
                        if(info.getDynamic().size()==0) {
                            showToast("没有更多数据");
                            return;
                        }else {
                            showToast("加载成功");
                            dynamicEntityList.addAll(info.getDynamic());
                        }
                    }else if(tag == 2){
                        showToast("刷新成功");
                        dynamicEntityList = null;
                        dynamicEntityList = info.getDynamic();
                    }else {
                        dynamicEntityList = null;
                        dynamicEntityList = info.getDynamic();
                    }
                    fillingData();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                if(isFirstIn) {
                    isFirstIn = false;
                    BasexUtilsProtocol.dialog.cancel();
                }
                if(tag!=0) {
                    ptr_listview.onRefreshComplete();
                }
            }
        });
    }

    public void fillingData(){
        if(isFirstIn) {
            adapter = new CommonBaseAdapter<ProClassWorkInfo.DynamicEntity>(this,dynamicEntityList,R.layout.item_classwork) {
                @Override
                public void convert(ViewHolder holder, int position) {
                    ProClassWorkInfo.DynamicEntity entity = dynamicEntityList.get(position);
                    long lasttime = entity.getLasttime();
                    String cnMonth = DateUtil.getCNMonth(DateUtil.getDateM(lasttime));
                    String dateD = DateUtil.getDateD(lasttime);
                    String dateYMDHM = DateUtil.getDateYMDHM(lasttime);
                    String type = "";
                    switch (entity.getType()) {
                        case 3:
                            type = "班级通知";
                            break;
                        case 4:
                            type = "班级作业";
                            break;
                        case 5:
                            type = "课程表";
                            break;
                    }
                    final String title = entity.getTitle();
                    final String joinid = entity.getJoinid()+"";
                    final String typeid = entity.getType()+"";
                    holder.setText(R.id.tv_day,dateD);
                    holder.setText(R.id.tv_month,cnMonth);
                    holder.setText(R.id.tv_type,type);
                    holder.setText(R.id.tv_time,dateYMDHM);
                    holder.setText(R.id.tv_looknum,"浏览"+entity.getLookNum()+"次");
                    holder.setText(R.id.tv_title,title);

                    //内容点击监听
                    holder.setOnclickListener(R.id.rl_content, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(ClassWorkActivity.this, AssignmentsAndCourseDetailsActivity.class);
                            intent.putExtra("title",title);
                            intent.putExtra("noticeId",joinid);
                            intent.putExtra("type",typeid);
                            startActivity(intent);
                        }
                    });
                    //浏览次数点击监听
                    holder.setOnclickListener(R.id.tv_looknum, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(ClassWorkActivity.this, ClassWorkLookPeopleNumActivity.class);
                            intent.putExtra("joinid",joinid);
                            intent.putExtra("type",typeid);
                            startActivity(intent);
                        }
                    });
                }
            };
            lv.setAdapter(adapter);
        }else {
            adapter.resetList(dynamicEntityList);
            adapter.notifyDataSetChanged();
        }

    }
}